library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.3     ✓ dplyr   1.0.2
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
library(leaflet)
library(htmltools)
library(htmlwidgets)
social_vulnerability <- st_read(
  "data_downloads/Climate_Ready_Boston_Social_Vulnerability-shp/3aeae140-8174-4d77-8c0e-de3ef0ce4b672020330-1-1rr22uq.veze.shp")
## Reading layer `3aeae140-8174-4d77-8c0e-de3ef0ce4b672020330-1-1rr22uq.veze' from data source `/Users/jocelyntsai/Desktop/GitHub/ctsai1-vis/data_downloads/Climate_Ready_Boston_Social_Vulnerability-shp/3aeae140-8174-4d77-8c0e-de3ef0ce4b672020330-1-1rr22uq.veze.shp' using driver `ESRI Shapefile'
## Simple feature collection with 180 features and 16 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -71.19116 ymin: 42.22789 xmax: -70.9232 ymax: 42.40494
## geographic CRS: WGS 84

Add a column for Total older adults/ total tract population

social_vulnerability <- social_vulnerability %>%
  mutate(percent_over65= (social_vulnerability$OlderAdult/social_vulnerability$POP100_RE))

Here you can find available basemaps in leaflet. Some of them will not work in R http://leaflet-extras.github.io/leaflet-providers/preview/.

This is how you can color polygons based on a specific value, similar to fill in ggplot.

Here, I’ll create a color palette. This website gives a detailed tutorial on how to do that, for categorical and continuous variables. https://rstudio.github.io/leaflet/colors.html

continuous_color_palette <- colorQuantile("Blues", social_vulnerability$percent_over65, n = 9)

map_1 <- leaflet(social_vulnerability) %>%
  addProviderTiles("Esri.WorldGrayCanvas") %>%
  addPolygons(fillColor = continuous_color_palette(social_vulnerability$percent_over65), stroke = FALSE, smoothFactor=0.2, fillOpacity = 0.7)

map_1
map_2 <- leaflet(social_vulnerability) %>%
  addProviderTiles("Esri.WorldGrayCanvas") %>%
  addPolygons(fillColor = continuous_color_palette(social_vulnerability$percent_over65), stroke = FALSE, smoothFactor=0.2, fillOpacity = 0.8,
              highlightOptions = highlightOptions(fillColor = "red", fillOpacity = 0.5),
              label = social_vulnerability$Name,
              popup = paste("Neighborhood:", social_vulnerability$Name, "<br/>",
                            "Tract population count:", social_vulnerability$POP100_RE, "<br/>",
                            "Tract population over 65 years old:", social_vulnerability$OlderAdult, "<br/>",
                            "Percentage of population over 65 years old:", format(round((social_vulnerability$percent_over65)*100,digits =2)),"%"))
                           
map_2

Find latitude and longitude boundary with: https://boundingbox.klokantech.com/

Create a continuous palette

pal_1 <- colorNumeric(
  palette = "Blues",
  domain = social_vulnerability$percent_over65)

map_4 <- leaflet(social_vulnerability, 
                 options=leafletOptions(minZoom = 10, maxZoom = 16)) %>%
  addProviderTiles("Esri.WorldGrayCanvas") %>%
  addPolygons(fillColor = pal_1(social_vulnerability$percent_over65), stroke = FALSE, smoothFactor=0.2, fillOpacity = 1,
              highlightOptions = highlightOptions(fillColor = "purple", fillOpacity = 0.5),
              label = social_vulnerability$Name,
              popup = paste("Neighborhood:", social_vulnerability$Name, "<br/>",
                            "Tract population count:", social_vulnerability$POP100_RE, "<br/>",
                            "Tract population over 65 years old:", social_vulnerability$OlderAdult, "<br/>",
                            "Percentage of population over 65 years old:", format(round((social_vulnerability$percent_over65)*100,digits =2)),"%")) %>%
  setMaxBounds( lng1 = -71.16,
                lat1 = 42.27,
                lng2 = -70.89,
                lat2 = 42.39) %>%
  addLegend(pal = pal_1,
             values = social_vulnerability$percent_over65,
             title = "Percentage of population over 65 years old",
            opacity = 1)
                           
map_4
colorbins <- colorBin(palette = "Blues",
                   domain = social_vulnerability$percent_over65,
                   bins = 9,
                   pretty = FALSE)

map_3 <- leaflet(social_vulnerability, 
                 options=leafletOptions(minZoom = 10, maxZoom = 16)) %>%
  addProviderTiles("Esri.WorldGrayCanvas") %>%
  addPolygons(fillColor = colorbins(social_vulnerability$percent_over65), stroke = FALSE, smoothFactor=0.2, fillOpacity = 1,
              highlightOptions = highlightOptions(fillColor = "purple", fillOpacity = 0.5),
              label = social_vulnerability$Name,
              popup = paste("Neighborhood:", social_vulnerability$Name, "<br/>",
                            "Tract population count:", social_vulnerability$POP100_RE, "<br/>",
                            "Tract population over 65 years old:", social_vulnerability$OlderAdult, "<br/>",
                            "Percentage of population over 65 years old:", format(round((social_vulnerability$percent_over65)*100,digits =2)),"%")) %>%
  
    addControl('<a href="https://bostonopendata-boston.opendata.arcgis.com/datasets/34f2c48b670d4b43a617b1540f20efe3_0?geometry=-71.551%2C42.227%2C-70.563%2C42.405&page=9">Data source</a>',
             position = "bottomleft") %>%
   addControl("Boston Older Adults", position = "topright") %>%

  setMaxBounds( lng1 = -71.16,
                lat1 = 42.27,
                lng2 = -70.89,
                lat2 = 42.39) %>%
  addLegend("bottomright",
            pal= colorbins,
            values = social_vulnerability$percent_over65,
            opacity = 1,
            title = "Percentage of population over 65 years old")
  
map_3

use the saveWidget() function to save any of leaflet maps as in its own html file.

saveWidget(map_3, file = "Boston_OlderAdults.html")